'use strict';
const fs = require('fs');
const path = require('path');
const Controller = require('egg').Controller;

class personsetController extends Controller {
  async avaterupload() {
    const { ctx, app } = this;
    const data = {
      "errno": 0,
      "data": {}
    }
    //接收上传上来的文件
    const file = ctx.request.files[0];
    console.log(file);

    const options = {
      where: {
        uid: ctx.session.uid
      }
    };

    //需要放到静态资源托管目录
    fs.copyFileSync(file.filepath, path.dirname(__dirname) + '/public/uploads/' + path.basename(file.filepath));
    data.data.url = 'http://127.0.0.1:81/public/uploads/' + path.basename(file.filepath);
    data.data.alt = file.filename;
    console.log(data.data.url);

    const row = {
      avater: data.data.url
    };
    const result = await app.mysql.update('users', row, options);
    ctx.session.avater = data.data.url;
    ctx.body = data;
  }


  // 一进来就请求
  async getinfo() {
    const { ctx } = this;
    let res = { code: 1, msg: '成功' };
    const result = await this.app.mysql.select('users', { // 搜索 post 表
      where: { status: '1', uid: ctx.session.uid }, // WHERE 条件
      columns: ['avater', 'username', 'introduce', 'gender', 'birthday'], // 要查询的表字段
    });
    console.log(result);
    res.imgUrl = result;
    ctx.body = res;
  }

  //修改昵称
  async nicknameupload() {
    const { ctx, app } = this;
    //接收上传上来的文件
    const resu = ctx.request.query;
    console.log('nick', resu);
    // ctx.session.uid = 1;

    const options = {
      where: {
        uid: ctx.session.uid
      }
    };
    const row = {
      username: resu.username,
    };
    const result = await app.mysql.update('users', row, options); // 更新 posts 表中的记录const result = await app.mysql.update('users', row); 更新 posts 表中的记录
    console.log(result);
    ctx.session.username = resu.username;

    ctx.body = resu.username;
  }

  // 修改个性签名
  async introduceupload() {
    const { ctx, app } = this;
    //接收上传上来的文件
    const resu = ctx.request.query;
    console.log(resu);
    // ctx.session.uid = 1;

    const options = {
      where: {
        uid: ctx.session.uid
      }
    };
    const row = {
      introduce: resu.introduce,
    };
    const result = await app.mysql.update('users', row, options); // 更新 posts 表中的记录const result = await app.mysql.update('users', row); 更新 posts 表中的记录
    console.log(result);
    ctx.body = resu.introduce;
  }

  // 修改资料 性别 生日
  async proupload() {
    const { ctx, app } = this;
    //接收上传上来的文件
    const resu = ctx.request.query;
    console.log(resu);
    // ctx.session.uid = 1;

    if (resu.gender == '男') {
      resu.gender = 1;
    } else if (resu.gender == '女') {
      resu.gender = 2;
    } else {
      resu.gender = 0;
    }
    const options = {
      where: {
        uid: ctx.session.uid,
      }
    };
    const row = {
      gender: resu.gender,
      birthday: resu.birthday,
    };
    const result = await app.mysql.update('users', row, options); // 更新 posts 表中的记录const result = await app.mysql.update('users', row); 更新 posts 表中的记录
    console.log(result);
    ctx.body = resu;
  }
}


module.exports = personsetController;